<template>
	<view class="body">
		<u-sticky bgColor="#fff">
			<view class="input-box">
				<u-search :placeholder="type=='物资管理'?'请输入物资名称搜索':'请输入项目名称搜索'" v-model="searchValue" @clear="clearSearch" @change="doSearch"
					:showAction="false" :actionStyle="actionStyle">
				</u-search>
			</view>
		</u-sticky>
		
		<view class="ul-list1-xh" v-if="type=='物资管理'">
			<view v-for="(item,index) in userList" :key="index" v-show="item.show">
				<view class="li">
					<view url="" hover-class="none" @click="setProject(item)">
						<view class="pic">
							<image src="https://www.zxsl.zxmap.cn/zxch-app/2023/07/21/67ff3e8f16234e43ad4728cfff08160a.png" style="width: 35rpx;" mode="widthFix"></image>
						</view>
						<view class="txt">
							<text>{{item.name}}<text v-show="type=='物资管理'"> / {{ item.code }}</text></text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="ul-list1-xh" v-if="type!=='物资管理'">
			<tki-tree ref="tkitree"  @loadMoreData="getNewList()" v-on:checkedProject="checkedPj" :loading="hasMore" :searchVal="searchValue" :range="userList" rangeKey="name" :selectParent="true" confirmColor="#4e8af7" ></tki-tree>
		</view>
		<view class="notYetclass" v-if="notYet">
			<text class="notYetclass-text">抱歉，暂未找到</text>
		</view>

		<view class="" style="margin-bottom: -120rpx;">

		</view>
	</view>
</template>

<script>
	import uniPopup from '@/subPackTask/components/choose/uni-popup/uni-popup.vue';
	import { list, listTF } from '@/api/oaProjectInfo.js'
	import { wzList } from '@/api/wzuse.js'
	import tkiTree from "@/components/tki-tree/tki-tree.vue"
	export default {
		data() {
			return {
				actionStyle: {
					color: '#007AFF',
					fontWeight: 'bold',
				},
				searchValue: '',
				userList: [],
				notYet: false,
				callBackFunc: '',
				type:'',
				tabList: [
					{
						name: '2024年前项目',
					}, 
					{
						name: '2024年项目',
					}
				],
				current: 0,
				hasMore: true,
				total: 0,
				pageSize: 20,// 分页大小
				pageNum: 1, //默认显示页面1
				isXmSearch: false,
			}
		},
		onLoad(option) {
			this.type = option.type;
			this.getProjects()
		},
		components: {
			uniPopup,
			tkiTree
		},
		methods: {
			//将数据返回上一页
			setProject(item) {
			    let pages = getCurrentPages();
			    let prevPage = pages[pages.length - 2]; //上一个页面
			    //直接调用上一个页面的setProject()方法，把数据存到上一个页面中去
				if(this.type=='物资管理') {
					prevPage.$vm.setProject({
						wzId: item.id,
						code: item.code,
						wzName: item.name,
						adminId:item.userId,
						adminName:item.userName,
					});
				}else {
					prevPage.$vm.setProject({
						id: item.id,
						name: item.name,
						fzrId:item.xmfzrId,
						fzrName:item.xmfzrName,
						jlTime:item.endTime,
						firstCompany:item.firstCompany,
						firstContacts:item.firstContacts,
						firstPhone:item.firstPhone,
						type:'项目'
					});
				}

				uni.navigateBack();
			},
			doSearch() {
				if (this.type=='物资管理') {
					if (this.searchValue === '') {
						this.clearSearch()
					}
					this.userList.map(e => {
						if (e.name.indexOf(this.searchValue) > -1) {
							e.show = true
						} else {
							e.show = false
						}
					})
				} else{
					this.isXmSearch = true
					this.pageNum = 1
					this.getProjects()
				}
			},
			clearSearch() {
				this.searchValue = ''
				if (this.type=='物资管理') {
					this.userList.map(e => e.show = true)
				}else{
					this.isXmSearch = false
				}
			},
			async getProjects() {
				if(this.type=='物资管理') {
					const { data } = await wzList({});
					if (data.code == 200) {
						this.userList = data.rows
						this.total = data.rows.length
						this.userList.map(e => e.show = true)
					}
				}else {
					let params
					if(this.isXmSearch){
						params = {
							pageNum:this.pageNum,
							pageSize:this.pageSize,
							name:this.searchValue
						}
					}else{
						params = {
							pageNum:this.pageNum,
							pageSize:this.pageSize,
						}
					}
					const { data } = await list(params);
					if (data.code == 200) {
						this.userList = data.data.rows
						this.total = data.data.total
					}
				}

			},
			// 请求新数据
			getNewList(){
				this.pageNum++
				// 请求结果数据的条数小于页面大小，表示后面没有数据了
				if(this.total < this.pageSize){
					this.hasMore = false
					console.log("没有更多数据")
				}
				this.getProjects()
			},
			sectionChange(item) {
				this.current = item.index;
				this.userList = []
				this.getProjects()
			},
			checkedPj(e){
				this.setProject(e[0])
			}
		}
	}
</script>

<style>
	.body {
		padding-bottom: 180rpx;
	}

	.input-box {
		width: 700rpx;
		margin: 0 auto;
		padding: 10rpx 30rpx;
		padding-right: 30rpx;
		background-color: #ffffff;
	}
	
	.notYetclass {
		width: 750rpx;
		height: 700rpx;
	}

	.notYetclass-text {
		text-align: center;
		line-height: 700rpx;
		color: #008CFF;
	}
	
	.ul-list1-xh {
		overflow: hidden;
		padding: 20rpx 50rpx 0 30rpx;
	}
	
	.ul-list1-xh .li {
		padding: 20rpx 0;
		overflow: hidden;
		position: relative;
	}
	
	.ul-list1-xh .li .pic {
		float: left;
		margin-right: 20rpx;
	}
	
	.ul-list1-xh .li .txt {
		overflow: hidden;
		font-size: 30rpx;
		padding-bottom: 20rpx;
		border-bottom: 1px solid rgb(242, 242, 242);
	}
</style>